//
//  GenerateQRCardView.m
//  QRMaster
//
//  Created by myApple on 16/6/2.
//  Copyright © 2016年 myApple. All rights reserved.
//

#import "GenerateQRCardView.h"
#import "anaylsistDimensional.h"
#import "ZXMultiFormatWriter.h"
#import "ZXImage.h"
#import "ZXingObjC.h"
#import "AutoLayoutFrame.h"
#import "UIImageView+ImageSize.h"

@interface GenerateQRCardView()
{
    NSData * imageData;
}

@property(nonatomic,strong)UIView * cardBackGroundView;//显示二维码第一块view

@property(nonatomic,strong)UIImageView * cardBoardImgView;//二维码边框imgview

@property(nonatomic,strong)UIImageView * cardImageView;//二维码显示view

@property(nonatomic,strong)UIImageView * logoImgView;

@property(nonatomic,strong)UIView * nameView;

@property(nonatomic,strong)UILabel * nameLabel;

@property(nonatomic,strong)UIImageView * timeImageView;

@property(nonatomic,strong)UIView * promtBackView;

@property(nonatomic,strong)UIImageView * promtImgView;

@property(nonatomic,strong)UILabel * promtLabel;

@property(nonatomic,strong)UIView * helpView;

@property(nonatomic,strong)UIButton * helpBtn;


@end

@implementation GenerateQRCardView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self addAllSubView];
    }
    
    return self;
}

-(void)addAllSubView
{
    [self addSubview:self.cardBackGroundView];
    [self addSubview:self.helpView];
    [self.cardBackGroundView addSubview:self.cardBoardImgView];
    [self.cardBackGroundView addSubview:self.cardImageView];
    [self.cardImageView addSubview:self.logoImgView];
    [self addSubview:self.nameView];
    [self.nameView addSubview:self.nameLabel];
    [self.nameView addSubview:self.timeImageView];
    [self addSubview:self.promtBackView];
    [self.promtBackView addSubview:self.promtImgView];
    [self.promtBackView addSubview:self.promtLabel];
    [self.helpView addSubview:self.helpBtn];
}

#pragma mark - initData
-(void)setGenerateCardString:(NSString *)generateCardString
{
    if (generateCardString == nil) {
        _logoImgView.hidden = YES;
        _cardImageView.image = [UIImage imageNamed:@"splash"];
        imageData = nil;
    }
    else
    {
        anaylsistDimensional * anaylsistDim = [[anaylsistDimensional alloc]init];
        NSString * encodeString = [anaylsistDim stringFromHexString:generateCardString];
        ZXImage * image = [anaylsistDim prepareCardView:encodeString addImgView:self.cardImageView];
        if (image)
        {
            _cardImageView.image = [UIImage imageWithCGImage:image.cgimage];
            UIImage * cardImg = [UIImage imageWithCGImage:image.cgimage];
            imageData = UIImageJPEGRepresentation(cardImg, 1.0);
            _logoImgView.hidden = NO;
        }
        else
        {
            self.cardImageView.image = [UIImage imageNamed:@"splash"];
            imageData = nil;
            _logoImgView.hidden = YES;
        }

    }
}

-(void)setRoomName:(NSString *)roomName
{
    _roomName = roomName;
    self.nameLabel.text = [NSString stringWithFormat:@"%@  %@  %@",_roomName,_floorName,_buildName];
    self.promtLabel.text = @"提示：开门时请将二维码对准门禁扫描器。当听见“滴”一声并亮蓝灯时，门禁打开。";
    [self.helpBtn setTitle:NSLocalizedString(@"cardHelp", nil) forState:0];
}

#pragma mark - prepareView
-(void)layoutSubviews
{
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat width = self.frame.size.width;
    CGFloat height =[AutoLayoutFrame autoSizeY:25] * 2 + 210;
    self.cardBackGroundView.frame = CGRectMake(x, y, width, height);
    
    x = (width-210)/2.0;
    y = [AutoLayoutFrame autoSizeY:25];
    width = 210;
    height = 210;
    self.cardBoardImgView.frame = CGRectMake(x, y, width, height);
    
    x = (self.frame.size.width - 180)/2.0;
    y = [AutoLayoutFrame autoSizeY:25] + 15;
    width = 180;
    height = 180;
    self.cardImageView.frame = CGRectMake(x, y, width, height);
    
    self.logoImgView.frame = CGRectMake((width - 30)/2.0, (height - 30)/2.0, 30, 30);
    
    x = 0;
    y = self.cardBackGroundView.frame.size.height;
    width = self.frame.size.width;
    height = [AutoLayoutFrame autoSizeY:44];
    self.nameView.frame = CGRectMake(x, y, width, height);
    
    x = 16;
    y = [AutoLayoutFrame autoSizeY:12];
    width = self.frame.size.width - 60;
    height = [AutoLayoutFrame autoSizeY:20];
    self.nameLabel.frame = CGRectMake(x, y, width, height);
    
    self.promtBackView.frame = CGRectMake(0, self.nameView.frame.size.height + self.nameView.frame.origin.y, self.frame.size.width, height);
    
    x = self.frame.size.width - 44;
    y = 0;
    width = 44;
    height = self.nameView.frame.size.height;
    _timeImageView.frame = CGRectMake(x, y, width, height);
    _timeImageView.edgeImage = CGRectMake(14, (self.nameView.frame.size.height -16)/2.0, 16, 16);
    
    //提示
    y = 15;
    x = 16;
    width = 16;
    height = 16;
    self.promtImgView.frame = CGRectMake(x, y, width, height);
    
    y = 15;
    x += width + 10;
    width = self.frame.size.width - x - 16;
    height = [self calculateWidth:self.promtLabel.text];
    self.promtLabel.frame = CGRectMake(x, y, width, height);
    
    //计算高度后 重新算promtbackviewframe
    self.promtBackView.frame = CGRectMake(0, _promtBackView.frame.origin.y, _promtBackView.frame.size.width, height + [AutoLayoutFrame autoSizeY:30]);
    
    y = self.promtBackView.frame.origin.y + self.promtBackView.frame.size.height + [AutoLayoutFrame autoSizeY:16];
    x = 0;
    width = self.frame.size.width;
    height = [AutoLayoutFrame autoSizeY:44];
    self.helpView.frame = CGRectMake(x, y, width, height);
    
    x = 0;
    y = (height - 20)/2.0;
    width = self.frame.size.width - 16;
    height = 20;
    self.helpBtn.frame = CGRectMake(x, y, width, height);
    
}

#pragma mark - lazy

-(UIView *)cardBackGroundView
{
    if (!_cardBackGroundView) {
        _cardBackGroundView = [[UIView alloc]init];
        _cardBackGroundView.backgroundColor = [UIColor whiteColor];
    }
    return _cardBackGroundView;
}

-(UIImageView *)cardBoardImgView
{
    if (!_cardBoardImgView) {
        _cardBoardImgView = [[UIImageView alloc]init];
        _cardBoardImgView.layer.borderColor = UIColorFromRGB(0xc1c1c1).CGColor;
        _cardBoardImgView.layer.borderWidth = 1.0;
    }
    return _cardBoardImgView;
}

-(UIImageView *)cardImageView
{
    if (!_cardImageView) {
        _cardImageView = [[UIImageView alloc]init];
        _cardImageView.userInteractionEnabled = YES;
    }
    return _cardImageView;
}

-(UIImageView *)logoImgView
{
    if (!_logoImgView) {
        _logoImgView = [[UIImageView alloc]init];
        _logoImgView.image = [UIImage imageNamed:@"ic_锁掌柜_70"];
    }
    return _logoImgView;
}

-(UIView *)nameView
{
    if (!_nameView) {
        _nameView = [[UIView alloc]init];
        _nameView.backgroundColor = UIColorFromRGB(0x7dc234);
    }
    return _nameView;
}

-(UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.font = [UIFont systemFontOfSize:16.0];
        _nameLabel.textColor = [UIColor whiteColor];
    }
    return _nameLabel;
}

-(UIImageView *)timeImageView
{
    if (!_timeImageView) {
        _timeImageView = [[UIImageView alloc]init];
        _timeImageView.image = [UIImage imageNamed:@"ic_time_20"];
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(lookTime)];
        _timeImageView.userInteractionEnabled = YES;
        [_timeImageView addGestureRecognizer:tap];
    }
    return _timeImageView;
}

-(UIView *)promtBackView
{
    if (!_promtBackView) {
        _promtBackView = [[UIView alloc]init];
        _promtBackView.backgroundColor = [UIColor whiteColor];
    }
    return _promtBackView;
}

-(UIImageView *)promtImgView
{
    if (!_promtImgView) {
        _promtImgView = [[UIImageView alloc]init];
    }
    return _promtImgView;
}

-(UILabel *)promtLabel
{
    if (!_promtLabel) {
        _promtLabel = [[UILabel alloc]init];
        _promtLabel.font = [UIFont systemFontOfSize:14.0];
        _promtLabel.numberOfLines = 0;
        _promtLabel.lineBreakMode = NSLineBreakByClipping;
    }
    return _promtLabel;
}

-(UIView *)helpView
{
    if (!_helpView) {
        _helpView = [[UIView alloc]init];
        _helpView.backgroundColor = [UIColor whiteColor];
    }
    return _helpView;
}

-(UIButton *)helpBtn
{
    if (!_helpBtn) {
        _helpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _helpBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [_helpBtn setTitleColor:UIColorFromRGB(0xff0000) forState:0];
        _helpBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_helpBtn addTarget:self action:@selector(helpInfo) forControlEvents:UIControlEventTouchUpInside];
    }
    return _helpBtn;
}

#pragma mark - 查看房间的生成二维码的时间
-(void)lookTime
{
    if (_delegate && [_delegate respondsToSelector:@selector(lookCardTime)]) {
        [_delegate lookCardTime];
    }
}

#pragma mark - 产看帮助提示
-(void)helpInfo
{
    if (_delegate && [_delegate respondsToSelector:@selector(showHelpView)]) {
        [_delegate showHelpView];
    }
}

#pragma mark - 计算高度
-(float)calculateWidth:(NSString *)str
{
     NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:14.0]};
    CGSize textSize = [str boundingRectWithSize:CGSizeMake(self.frame.size.width - 58, 60) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    float height = textSize.height;
    return height;
}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
